<template>
  <div class="search-box">
    <i class="icon-search"></i>
    <input type="text" :placeholder="placeholder" v-model="query" ref="query">
    <i class="icon-dismiss" @click="clear" v-show="query.length"></i>
  </div>
</template>
<script>
import {debounce} from 'common/js/util'
export default {
  name: 'SearchBox',
  props: {
    placeholder: {
      type: String,
      default: '搜索歌曲、歌手'
    }
  },
  data () {
    return {
      query: ''
    }
  },
  methods: {
    blur () {
      this.$refs.query.blur()
    },
    clear () {
      this.query = ''
    },
    SetQuery (query) {
      this.query = query
    }
  },
  created () {
    this.$watch('query', debounce((newQuery) => {
      this.$emit('query', newQuery)
    }, 200))
  }
}
</script>
<style lang="scss" scoped>
@import 'common/scss/variable.scss';
.search-box{
  display: flex;
  align-items: center;
  width: 100%;
  padding: 0 0.12rem;
  box-sizing: border-box;
  height: 0.8rem;
  background: $color-highlight-background;
  border-radius: 0.12rem;
  .icon-search{
    font-size: 0.48rem;
    color: $color-background;
  }
  input{
    flex: 1;
    margin: 0 0.1rem;
    line-height: 0.36rem;
    background: $color-highlight-background;
    color: $color-text;
    font-size: 0.28rem;
  }
  .icon-dismiss{
    font-size: 0.32rem;
    color: $color-background;
  }
}
</style>
